package com.shuma.product.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuma.product.mapper.GoodsBaseInfoMapper;
import com.shuma.product.pojo.GoodsBaseInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    private static final Logger LOG = LoggerFactory.getLogger(RedisKeyExpirationListener.class);

    @Autowired
    private GoodsBaseInfoMapper goodsBaseInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对redis数据失效事件，进行数据处理
     *
     * @param message 失效的key
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String key = message.toString();
        LOG.info("在Redis中过期ID{}", key);
        if (key.startsWith("autoToOnline:")) {
            key = key.substring(13);
            extracted(key);
        } else if (key.startsWith("autoToOffline:")) {
            key = key.substring(14);
            extracted(key);
        }

    }

    private void extracted(String key) {
        GoodsBaseInfo goodsBaseInfo = getGoodsBaseInfo(key);
        if (goodsBaseInfo != null) {
            // 过期处理逻辑
            /**
             * 实现商品自动上线
             * 根据ID去数据库中查这个商品
             * 1.店小二操作状态为3 (1.草稿、2已编辑、3.已提交审核)
             * 2.商品审核状态为2 (1.未审核、2.审核通过、3.审核驳回)
             * 3.商品上线状态为1(自动上线) 为2(自动下线) (1.未上线、2.已上线、3.已下线)
             */
            Integer goodsOwnerStatus = goodsBaseInfo.getGoodsOwnerStatus();
            Integer goodsCheckStatus = goodsBaseInfo.getGoodsCheckStatus();
            Integer goodsOnlineStatus = goodsBaseInfo.getGoodsOnlineStatus();
            // 实现商品自动上下线
            if (goodsOwnerStatus.equals(3) && goodsCheckStatus.equals(2)) {
                // 上线
                if (goodsOnlineStatus.equals(1)) {
                    goodsBaseInfo.setGoodsOnlineStatus(2);
                    LOG.info("ID为{}的商品实现开始自动上线", key);
                    goodsBaseInfoMapper.updateById(goodsBaseInfo);
                }
                // 下线
                else if (goodsOnlineStatus.equals(2)) {
                    goodsBaseInfo.setGoodsOnlineStatus(3);
                    LOG.info("ID为{}的商品实现开始自动下线", key);
                    goodsBaseInfoMapper.updateById(goodsBaseInfo);
                }
            }
        }
    }

    private GoodsBaseInfo getGoodsBaseInfo(String key) {
        // 判断该ID是否为商品
        LambdaQueryWrapper<GoodsBaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsBaseInfo::getGoodsBaseId, key);
        GoodsBaseInfo goodsBaseInfo = goodsBaseInfoMapper.selectOne(queryWrapper);
        return goodsBaseInfo;
    }

}
